package managedbean.MRP.SaleFigure;

import entity.MRP.SaleForecast.AnnualSaleFigure;
import entity.MRP.SaleForecast.MonthlySaleFigure;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent; 
import javax.faces.event.PhaseEvent;
import session.stateless.MRP.SaleForecast.SaleFigureBeanLocal;

@ManagedBean(name="viewSaleFigureManagerBean")
@ViewScoped
public class ViewSaleFigureManagerBean implements Serializable {
    @EJB 
    private SaleFigureBeanLocal saleFigureBeanLocal; 
    
    private MonthlySaleFigure monthlySaleFigure;
    
    private int saleFigureYear;
    private List<Integer> saleFigureYears;
    private List<MonthlySaleFigure> monthlySaleFigures;   
    private MonthlySaleFigure selectedMonthlySaleFigure;
    private String statusMessage;
    
    public int getSaleFigureYear() {
        return saleFigureYear;
    }

    public void setSaleFigureYear(int saleFigureYear) {
        this.saleFigureYear = saleFigureYear;
    }

    public List<Integer> getSaleFigureYears() {
        saleFigureYears = new ArrayList<Integer>();
        for (AnnualSaleFigure a: saleFigureBeanLocal.getAnnualSaleFigures())
        {
            saleFigureYears.add(a.getSaleFigureYear());
        } 
        Collections.reverse(saleFigureYears);
        return saleFigureYears;
    }

    public List<MonthlySaleFigure> getMonthlySaleFigures() {
        return monthlySaleFigures;
    }

    public void setMonthlySaleFigures(List<MonthlySaleFigure> monthlySaleFigures) {
        this.monthlySaleFigures = monthlySaleFigures;
    }

    public MonthlySaleFigure getSelectedMonthlySaleFigure() {
        return selectedMonthlySaleFigure;
    }

    public void setSelectedMonthlySaleFigure(MonthlySaleFigure selectedMonthlySaleFigure) {
        this.selectedMonthlySaleFigure = selectedMonthlySaleFigure;
    }
    
    public void initView(PhaseEvent event){
    }  
    
    public String getMonth(int month){
        if (month == 0) return "January";
        if (month == 1) return "February";
        if (month == 2) return "March";
        if (month == 3) return "April";
        if (month == 4) return "May";
        if (month == 5) return "June";
        if (month == 6) return "July";
        if (month == 7) return "August";
        if (month == 8) return "September";
        if (month == 9) return "October";
        if (month == 10) return "November";
        return "December";
    }
    
    public void retrieveData(ActionEvent event){ 
        Calendar currentDate = Calendar.getInstance();
        int currentYear = currentDate.get(Calendar.YEAR);
        int currentMonth = currentDate.get(Calendar.MONTH);
        if (this.getSaleFigureYear() != currentYear){
            this.setMonthlySaleFigures(saleFigureBeanLocal.getMonthlySaleFigures(this.getSaleFigureYear(), 0, 11));
        }
        else{
            if (currentMonth != 0){
                this.setMonthlySaleFigures(saleFigureBeanLocal.getMonthlySaleFigures(this.getSaleFigureYear(), 0, currentMonth));
            }
            else{
                this.setMonthlySaleFigures(null);
            }
        }
    }   
    
    public void updateSaleFigure(ActionEvent event){
        try{
            System.out.println(selectedMonthlySaleFigure.getId());
            saleFigureBeanLocal.editSaleFigure(selectedMonthlySaleFigure.getId(), selectedMonthlySaleFigure.getSaleFigureLineItems());
            statusMessage = "Sale Figure Updated Successfully.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Sale Figure Result: " + statusMessage, ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
        
    public void validateQuantity(FacesContext context, UIComponent toValidate, Object value){
        
        long qty = -1;
        try{
            qty = (Long) value;
        }
        catch(Exception e){
            
        }
        boolean check = true;
        if (qty < 0)
        {
            check = false;
        }
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Quantity is invalid. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }  
}
